package org.ww.product.app;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.ww.common.utils.PageUtils;
import org.ww.common.utils.R;
import org.ww.product.entity.BrandEntity;
import org.ww.product.entity.CategoryBrandRelationEntity;
import org.ww.product.service.CategoryBrandRelationService;
import org.ww.product.vo.BrandVo;

import java.util.*;
import java.util.stream.Collectors;


/**
 * 品牌分类关联
 *
 * @author ww
 * @email ww@qq.com
 * @date 2024-01-05 13:25:17
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;


    /**
     * 根据分类id查询品牌列表
     */
    @GetMapping("/brands/list")
    public R brandList(@RequestParam(value = "catId",required = true) Long catId){
        List<BrandEntity> brandList = categoryBrandRelationService.getBrandListByCatId(catId);
        List<BrandVo> vos = Optional.ofNullable(brandList)
                .orElseGet(ArrayList::new)
                .stream()
                .map(b -> {
                    BrandVo vo = new BrandVo();
                    vo.setBrandId(b.getBrandId());
                    vo.setBrandName(b.getName());
                    return vo;
                }).collect(Collectors.toList());
        return R.ok().put("data", vos);
    }

    /**
     * 列表
     */
    @GetMapping("/catelog/list")
    public R catelogList(@RequestParam("brandId") Long brandId){
        LambdaQueryWrapper<CategoryBrandRelationEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CategoryBrandRelationEntity::getBrandId,brandId);
        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(wrapper);
        return R.ok().put("data", data);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.saveDetails(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
